package io.renren;

import io.renren.common.utils.IPUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;


@SpringBootApplication
public class Application extends SpringBootServletInitializer {

	private static Logger logger = LoggerFactory.getLogger(Application.class);
	public static void main(String[] args) {
		ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

		printHomeUrl(applicationContext);
	}

	/**
	 * 打印首页地址
	 *
	 * @param applicationContext
	 */
	private static void printHomeUrl(ConfigurableApplicationContext applicationContext) {
		String localIpAddress = IPUtils.getLocalIpAddress();
		String host = StringUtils.isNotEmpty(localIpAddress) ? localIpAddress : "localhost";
		String port = getProperty(applicationContext, "server.port");
		String contextPath = getProperty(applicationContext, "server.context-path");
		logger.info("Home Page => http://{}:{}{}", host, port, contextPath);
	}

	private static String getProperty(ConfigurableApplicationContext applicationContext, String key) {
		return applicationContext.getEnvironment().getProperty(key);
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Application.class);
	}
}